package com.itbaizhan.shopping_goods_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbaizhan.shopping_common.pojo.*;
import com.itbaizhan.shopping_common.service.GoodsService;
import com.itbaizhan.shopping_common.service.SearchService;
import com.itbaizhan.shopping_goods_service.mapper.GoodsImageMapper;
import com.itbaizhan.shopping_goods_service.mapper.GoodsMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品服务实现类
 */
@DubboService
@Transactional
@Service
public class GoodsServiceImpl implements GoodsService {
	
	@Autowired
	private GoodsMapper goodsMapper;
	@Autowired
	private GoodsImageMapper goodsImageMapper;
	@Autowired
	private RocketMQTemplate rocketMQTemplate;//工具类
	
	//同步商品数据主题
	private final String SYNC_GOODS_QUEUE = "sync_goods_queue";
	
	//删除商品数据主题
	private final String DEL_GOODS_QUEUE = "del_goods_queue";
	
	//修改购物车数据主题
	private final String SYNC_CART_QUEUE = "sync_cart_queue";
	
	//删除购物车数据主题
	private final String DEL_CART_QUEUE = "del_cart_queue";
	
	/**
	 * 添加商品
	 *
	 * @param goods
	 */
	public void add(Goods goods) {
		// 插入商品数据
		goodsMapper.insert(goods);
		
		// 插入图片数据
		Long goodsId = goods.getId(); // 获取商品主键
		List<GoodsImage> images = goods.getImages(); // 商品图片
		for (GoodsImage image : images) {
			image.setGoodsId(goodsId); // 给图片设置商品id
			goodsImageMapper.insert(image); //插入图片
		}
		
		// 插入商品_规格项数据
		// 1.获取规格
		List<Specification> specifications = goods.getSpecifications();
		// 2.获取规格项
		List<SpecificationOption> options = new ArrayList(); //规格项集合
		// 遍历规格，获取规格中的所有规格项
		for (Specification specification : specifications) {
			options.addAll(specification.getSpecificationOptions());
		}
		// 3.遍历规格项，插入商品_规格项数据
		for (SpecificationOption option : options) {
			goodsMapper.addGoodsSpecificationOption(goodsId,option.getId());
		}
		// 同步商品到ES
		GoodsDesc desc = findDesc(goodsId);// 获取商品详情
		//searchService.syncGoodsToES(desc);
		rocketMQTemplate.syncSend(SYNC_GOODS_QUEUE, desc);//向MQ发送消息
	}
	
	/**
	 * 修改商品
	 *
	 * @param goods
	 */
	public void update(Goods goods) {
		// 删除旧图片数据
		Long goodsId = goods.getId(); // 商品id
		QueryWrapper<GoodsImage> queryWrapper = new QueryWrapper();
		queryWrapper.eq("goodsId",goodsId);
		goodsImageMapper.delete(queryWrapper);
		// 删除旧规格项数据
		goodsMapper.deleteGoodsSpecificationOption(goodsId);
		
		
		// 插入商品数据
		goodsMapper.updateById(goods);
		// 插入图片数据
		List<GoodsImage> images = goods.getImages(); // 商品图片
		for (GoodsImage image : images) {
			image.setGoodsId(goodsId); // 给图片设置商品id
			goodsImageMapper.insert(image); // 插入图片
		}
		// 插入商品_规格项数据
		List<Specification> specifications = goods.getSpecifications(); // 获取规格
		List<SpecificationOption> options = new ArrayList(); // 规格项集合
		// 遍历规格，获取规格中的所有规格项
		for (Specification specification : specifications) {
			options.addAll(specification.getSpecificationOptions());
		}
		// 遍历规格项，插入商品_规格项数据
		for (SpecificationOption option : options) {
			goodsMapper.addGoodsSpecificationOption(goodsId,option.getId());
		}
		
		// 同步商品到ES
		GoodsDesc desc = findDesc(goodsId);// 获取商品详情
		//searchService.syncGoodsToES(desc);
		rocketMQTemplate.syncSend(SYNC_GOODS_QUEUE, desc);//向MQ发送消息，ES服务监听
		// 将商品数据同步到购物车中
		CartGoods cartGoods = new CartGoods();
		cartGoods.setGoodId(goods.getId());
		cartGoods.setGoodsName(goods.getGoodsName());
		cartGoods.setHeaderPic(goods.getHeaderPic());
		cartGoods.setPrice(goods.getPrice());
		rocketMQTemplate.syncSend(SYNC_CART_QUEUE,cartGoods);//向MQ发送消息，购物车服务监听
		
	}
	
	
	
	/**
	 * 修改商品上架状态
	 *
	 * @param id           商品id
	 * @param isMarketable 是否上架
	 */
	public void putAway(Long id, Boolean isMarketable) {
		
		goodsMapper.putAway(id, isMarketable);
		//1.上架时数据同步到ES，下架时数据从ES删除
		//2.上架数据时从购物车中同步数据，下架时从购物车中删除数据
		if (isMarketable) {
			
			GoodsDesc desc = findDesc(id);// 获取商品详情
			rocketMQTemplate.syncSend(SYNC_GOODS_QUEUE, desc);//向MQ发送消息，ES服务监听
			
		}else {
			//下架
			rocketMQTemplate.syncSend(DEL_GOODS_QUEUE, id);//向MQ发送消息，ES服务监听
			rocketMQTemplate.syncSend(DEL_CART_QUEUE,id);//向MQ发送消息，购物车服务监听
			
		}
	}
	
	/**
	 * 根据商品id查询商品
	 *
	 * @param id
	 * @return
	 */
	public Goods findById(Long id) {
		return goodsMapper.findById(id);
	}
	
	/**
	 * 商品搜索
	 *
	 * @param goods 查询条件
	 * @param page  页码
	 * @param size  每页记录数
	 * @return
	 */
	public Page<Goods> search(Goods goods, int page, int size) {
		
		QueryWrapper<Goods> wrapper = new QueryWrapper<>();
		//判断商品是否为空
		if (goods != null && StringUtils.hasText(goods.getGoodsName())) {
			//goods.getGoodsName() != null && !goods.getGoodsName().equals("")
			wrapper.like("goodsName", goods.getGoodsName());
		}
		Page<Goods> goodsPage = goodsMapper.selectPage(new Page<>(page, size), wrapper);
		
		return goodsPage;
	}
	
	/**
	 * 查询所有商品详情
	 * @return
	 */
	public List<GoodsDesc> findAll() {
		return goodsMapper.findAll();
	}
	
	/**
	 * 根据商品id查询商品详情
	 * @param id
	 * @return
	 */
	public GoodsDesc findDesc(Long id) {
		return goodsMapper.findDesc(id);
	}
}
